<%--
  Created by IntelliJ IDEA.
  User: 22328
  Date: 2021/2/8
  Time: 15:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>商品添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form layui-form-pane" onsubmit="return false">
            <div class="layui-form-item">
                <label for="goods_name" class="layui-form-label">
                    商品名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="goods_name" name="goods_name" value="" placeholder="请输入商品名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="goods_nums" class="layui-form-label">
                    商品数量
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="goods_nums" name="goods_nums" value="" placeholder="商品的数量(单位/件)"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="goods_price" class="layui-form-label">
                    商品单价
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="goods_price" name="goods_price" value="" placeholder="商品的单价(单位/元)"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="goods_img" class="layui-form-label">
                    商品图片
                </label>
                <div class="layui-input-inline">
                    <input style="display: none" onchange="viewImg(this)" type="file" accept="image/jpeg" id="goods_img" name="goods_img" value="上传图片" class="layui-input">
                    <div id="upload_msg"  style="padding-top: 5px;padding-left: 10px"><i class="layui-icon">&#xe67c;</i>上传图片</div>
                    <img src="" id="img_view"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="goods_desc" class="layui-form-label">
                    商品描述
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="goods_desc" name="goods_desc" value="" placeholder="商品的详细描述"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品分类</label>
                <div class="layui-input-inline">
                    <select id="goods_type" name="goods_type">
                        <option value="请选择">请选择</option>
                        <option value="1">衣物</option>
                        <option value="2">美妆</option>
                        <option value="3">书籍</option>
                        <option value="4">电子</option>
                        <option value="5">玩具</option>
                        <option value="6">食物</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <label for="goods_img" class="layui-form-label">
                    商家ID
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="userId" name="userId" value="${sessionScope.user.id}" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <button id="add_btn" class="layui-btn">
                    上架
                </button>
            </div>
        </form>
    </div>
</div>
</body>
<script src="./js/jquery-3.1.0.min.js" type="text/javascript"></script>
<script>
    function viewImg(e){
        var ele = document.getElementById('img_view');
        ele.setAttribute("style","width:150px;height:170px");
        ele.src=window.URL.createObjectURL(e.files[0]);
    }

    layui.use(['form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer;
    });
    $(function () {
        $("#upload_msg").click(function () {
            $("#goods_img").click();
        });

        $("#add_btn").click(function () {
            var userId = $("#userId").val();
            var goodsName = $("#goods_name").val();
            var goodsNum = $("#goods_nums").val();
            var goodsPrice = $("#goods_price").val();
            var goodsImg = $("#goods_img")[0].files[0];
            var goodsDesc = $("#goods_desc").val();
            var categoryId = $("#goods_type").val();
            var formData = new FormData();
            formData.append("userId",userId);
            formData.append("goodsName",goodsName);
            formData.append("goodsNum",goodsNum);
            formData.append("goodsPrice",goodsPrice);
            formData.append("goodsDesc",goodsDesc);
            formData.append("categoryId",categoryId);
            formData.append("goodsImg",goodsImg);
            $.ajax({
                url: "./addGoods",
                type: "post",
                data: formData,
                contentType: false,
                processData:false,
                success: function (res) {
                    if (res.data === "添加成功") {
                        layer.alert("添加成功", {
                                icon: 6
                            },
                            function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    } else {
                        layer.msg("添加失败");
                    }
                },
                error: function () {
                    alert("404.html页面");
                }
            });
        });
    });
</script>
</html>

